import os,ctypes
def chageWorkName():
    print('当前目录\t\t>>>>>>\t'+os.getcwd())
    if os.path.exists('d:\\') == True and os.path.exists('d:\\NewSiteForPython') == False:
        path='d:\\NewSiteForPython'
        os.mkdir(path)
        os.chdir(path)
        print(os.getcwd())
        return path
    elif os.path.exists('c:\\')==True and os.path.exists('c:\\NewSiteForPython')==False:
        path=("d:\\NewSiteForPython")
        os.chdir(path)
        print('现在目录\t\t>>>>>>\t'+os.getcwd())
        #print('1')
        return path
    else:
        return print("创建目录失败请重试")
def createSite(path):
    #创建Web目录
    print("开始创建站点：")
    guwangWangPath=(path+'\\XPJ_New_Offical')
    agentBackEndPath = (path + '\\XPJ_New_AgentBackEnd')

    if os.path.exists(guwangWangPath)==True:
        print("目录已经存在>>>>"+guwangWangPath)
    else:
        os.mkdir(guwangWangPath)
        print(guwangWangPath+"\t>>>>创建成功")
    if os.path.exists(agentBackEndPath):
        print("目录已经存在>>>>\t"+agentBackEndPath)
    else:
        os.mkdir(agentBackEndPath)
        print(agentBackEndPath+"\t>>>>创建成功")
    ##创建站点
    site1Res=os.system('%systemroot%/system32/Inetsrv/APPCMD.exe add site /name:"XPJ_New_Offical_Py" /bindings:"http/*:8444:" /physicalPath:'+guwangWangPath+' ')
    site2Res=os.system('%systemroot%/system32/Inetsrv/APPCMD.exe add site /name:"XPJ_New_agentBackEnd_py" /bindings:"http/*:8555:" /physicalPath:'+ agentBackEndPath+' ')
    if site1Res==0 and site2Res==0:
	    print('\n\t<<<<<<<<<<<<<<<<<站点创建成功>>>>>>>>>>>>>>>>>>>>>>\n')
    else:
        print('\n\t<<<<<<<<<<<<<<<<<站点创建失败>>>>>>>>>>>>>>>>>>>>>>\n')
    ##创建应用池
    os.system('%systemroot%/system32/Inetsrv/APPCMD.exe add apppool /name:"XPJ_New_Offical_PyAppPool" ')
    os.system('%systemroot%/system32/Inetsrv/APPCMD.exe add apppool /name:"XPJ_New_agentBackEnd_pyAppPool" ')
    site1Res=os.system("%systemroot%/system32/Inetsrv/APPCMD.exe set app XPJ_New_Offical_Py/ -applicationPool:XPJ_New_Offical_PyAppPool")
    site2Res = os.system("%systemroot%/system32/Inetsrv/APPCMD.exe set app XPJ_New_agentBackEnd_py/ -applicationPool:XPJ_New_agentBackEnd_pyAppPool")
    #if site1Res == 0 and site2Res == 0:
    print('\n\t<<<<<<<<<<<<<<<<<应用池创建成功>>>>>>>>>>>>>>>>>>>>>>\n')
    #else:
     #   print('\n\t<<<<<<<<<<<<<<<<<应用池创建失败>>>>>>>>>>>>>>>>>>>>>>\n')
    ##启动站点
    site1Res   = os.system('%systemroot%/system32/Inetsrv/APPCMD.exe start site "XPJ_New_Offical_Py" ')
    site2Res = os.system('%systemroot%/system32/Inetsrv/APPCMD.exe start site "XPJ_New_Offical_Py" ')
    #print(site2Res)
    if site1Res == 0 and site2Res == 0:
        print('\n\t<<<<<<<<<<<<<<<<<站点启动成功>>>>>>>>>>>>>>>>>>>>>>\n')
    else:
        print('\n\t<<<<<<<<<<<<<<<<<站点启动失败>>>>>>>>>>>>>>>>>>>>>>\n')
def downloadURL(URL,path):
    ###下载 svn代码执行工具及dll
    import urllib.request
    print("当前目录====="+path+"：>>>>>>正在下载SVN...........")
    f = urllib.request.urlopen(URL)
    date =f.read()
    with open(path+'\svn.zip','wb') as code :
        code.write(date)
        print("当前目录====="+path+"：>>>>>>正在解压SVN...........")
        return  path+'\svn.zip'
def unzip(path):
    #解压zip
    import zipfile
    f = zipfile.ZipFile(path, 'r')
    for file in f.namelist():
        f.extract(file, os.path.dirname(path))
    return os.path.dirname(path)
def svn(path):
    #svn checkout
    os.chdir(os.getcwd()+"\\XPJ_New_Offical")
    print(os.getcwd())
    os.system(svnExePath+" checkout --username Chasel --password C12345 https://DESKTOP-O6NB0OJ:8443/svn/TestSVN/Test1")


if __name__ == '__main__':

    URL='http://127.0.0.1/svn.zip'
    path=chageWorkName()
    createSite(path)
    svnPath=downloadURL(URL,path)
    unzip(svnPath)

    svnExePath=os.path.dirname(svnPath)+'\svn.exe'
    svnPath=os.path.dirname(svnPath)
    print('SVN程序所在的路径：'+svnExePath)
    svn(svnPath)
